package mz.matafe.bolao

import org.codehaus.groovy.grails.plugins.springsecurity.Secured

/**
 * Cup Controller.
 *
 * <p>Handle the cups
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class CupController extends BolaoController{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    @Secured(['ROLE_ADMIN','ROLE_SUPERVISOR'])
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [cups: Cup.list(params), cupTotal: Cup.count()]
    }

    @Secured(['ROLE_ADMIN'])
    def create = {
        def cup = new Cup()
        cup.properties = params
        return [cup: cup]
    }

    @Secured(['ROLE_ADMIN'])
    def save = {
        def cup = new Cup(params)
        if (cup.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'cup.label', default: 'Cup'), cup.year])}"
            redirect(action: "show", id: cup.id)
        }
        else {
            render(view: "create", model: [cup: cup])
        }
    }

    @Secured(['ROLE_ADMIN','ROLE_SUPERVISOR'])
    def show = {
        def cup = Cup.get(params.id)
        if (!cup) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'cup.label', default: 'Cup'), params.id])}"
            redirect(action: "list")
        }
        else {
            [cup: cup]
        }
    }

    @Secured(['ROLE_ADMIN'])
    def edit = {
        def cup = Cup.get(params.id)
        if (!cup) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'cup.label', default: 'Cup'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [cup: cup]
        }
    }

    @Secured(['ROLE_ADMIN'])
    def update = {
        def cup = Cup.get(params.id)
        if (cup) {
            if (params.version) {
                def version = params.version.toLong()
                if (cup.version > version) {
                    cup.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'cup.label', default: 'Cup')] as Object[], "Another user has updated this Cup while you were editing")
                    render(view: "edit", model: [cup: cup])
                    return
                }
            }
            cup.properties = params
            if (!cup.hasErrors() && cup.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'cup.label', default: 'Cup'), cup.year])}"
                redirect(action: "show", id: cup.id)
            }
            else {
                render(view: "edit", model: [cup: cup])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'cup.label', default: 'Cup'), params.id])}"
            redirect(action: "list")
        }
    }

    @Secured(['ROLE_ADMIN'])
    def delete = {
        def cup = Cup.get(params.id)
        if (cup) {
            try {
                cup.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'cup.label', default: 'Cup'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'cup.label', default: 'Cup'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'cup.label', default: 'Cup'), params.id])}"
            redirect(action: "list")
        }
    }
}
